package apps.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import webframe.common.helper.ControllerHelper;
import webframe.common.helper.FormatTransformHelper;
import apps.web.controller.base.MyBaseCommandController;
import apps.web.model.Apply;
import apps.web.model.EntUser;
import apps.web.model.UploadFile;

public class EntApplyEditController extends MyBaseCommandController {

	@Override
	protected ModelAndView handleInner(HttpServletRequest request, HttpServletResponse response,
			Object command) throws RuntimeException {
		Apply apply = (Apply) command;
		String tempId = request.getParameter("tempId");
		EntUser user = (EntUser) ControllerHelper.getUserBySession();

		apply.setApplyDate(new Date());
		apply.setEntRegCode(user.getDeclRegNo());
		apply.setAgentEntName(user.getEntCname());
		apply.setEntUserId(user.getId());
		apply.setDelFlag(0L);
		apply.setExhProp(FormatTransformHelper.getStringFromArray(request
				.getParameterValues("exhProp")));
		apply.setTrePlan(FormatTransformHelper.getStringFromArray(request
				.getParameterValues("trePlan")));
		apply.setItemSort(FormatTransformHelper.getStringFromArray(request
				.getParameterValues("itemSort")));
		apply.setSheetType(FormatTransformHelper.getStringFromArray(request
				.getParameterValues("sheetType")));
		apply.setApplyFlag(1L);

		apply = getServiceContainer().getApplyBizService().saveApply(apply);
		// 更新img file
		List<UploadFile> fileList = getServiceContainer().getFileBizService().findImgListBytempId(
				tempId);
		for (UploadFile uploadFile : fileList) {
			uploadFile.setApplyId(apply.getId());
			uploadFile.setUserid(user.getId());
			uploadFile.setTempId("");
		}
		getServiceContainer().getFileBizService().updateImgFiles(fileList);
		return null;
	}
}
